package com.example.demo.utils;

import com.example.demo.entity.Plane;

import java.text.DecimalFormat;
import java.util.HashMap;

public class PHValueCount {
    //计算健康值PHValue
    public static String phValue(Plane plane) {
        HashMap<String, Integer> weatherMap = new HashMap<>();
        weatherMap.put("优", 4);
        weatherMap.put("良", 3);
        weatherMap.put("中", 2);
        weatherMap.put("差", 1);
        String Weather = plane.getWeather();
        boolean flag = weatherMap.containsKey(Weather); //判断key是否存在
        if (!flag) {
            return "天气状况输入错误";
        }
        Integer weatherValue = weatherMap.get(Weather); //天气质量转换值
        double inTemp = plane.getInTemp();
        double outTemp = plane.getOutTemp();
        double value = Math.abs(inTemp - outTemp); //温度差
        Integer speedSum = 0;
        for (Integer speed : plane.getSpeed()) {
            Integer changeValue = calculateSpeed(plane.getEngineType(), speed); // 转速转换值
            if (changeValue == 0) {
                return "飞机型号不匹配";
            }
            speedSum += changeValue;
        }
        int speedAvg = speedSum / plane.getSpeed().size(); //计算转速平均值
        double phValue = speedAvg / (value * weatherValue); //计算健康值
        DecimalFormat decimalFormat = new DecimalFormat("#0.0000"); //保留四位小数
        String formatPHValue = decimalFormat.format(phValue);
        return formatPHValue;
    }

    //获取转换值方法
    private static Integer calculateSpeed(String engineType, Integer speed) {

        switch (engineType) {
            case "T3":
                if (speed < 3000)
                    return 30;
                else if (speed <= 5000)
                    return 60;
                else
                    return 100;
            case "T5":
                if (speed < 3000)
                    return 20;
                else if (speed <= 5000)
                    return 40;
                else
                    return 60;
            case "E6":
                if (speed < 3000)
                    return 10;
                else if (speed <= 5000)
                    return 20;
                else
                    return 50;
        }

        return 0;
    }
}
